package com.dataexa.ky.core.utils.exception;

import cn.hutool.core.lang.Assert;

public class SystemException extends RuntimeException {
    private String code;

    public SystemException(String message) {
        super(message);
        this.code = SystemCode.ERR_OTHER_SYSTEM_ERROR.getCode();
    }

    public SystemException(SystemCode code) {
        super(code.getText());
        if (code == SystemCode.SUCCESS) {
            throw new IllegalArgumentException("系统异常不能设置该错误码");
        } else {
            this.code = code.getCode();
        }
    }

    public SystemException(SystemCode code, String message) {
        super(message != null && message.trim().length() != 0 ? message.trim() : code.getText());
        if (code == SystemCode.SUCCESS) {
            throw new IllegalArgumentException("系统异常不能设置该错误码");
        } else {
            this.code = code.getCode();
        }
    }

    public SystemException(int code, String message) {
        super(message);
        Assert.isTrue(99 < code && code < 1000, "code 必须大于 99，小于 1000");
        this.code = String.valueOf(code);
    }

    public String getCode() {
        return this.code;
    }
}
